package com.boot.down.util.redis;

import redis.clients.jedis.Jedis;

import static com.boot.down.util.redis.LockConstants.*;


/**
 * 加锁与解锁(释放锁) 测试一(这种情况是分布式的服务都正常的情况下)
 * @author dell
 */
public class LockCase1 extends RedisLock {


	public LockCase1(Jedis jedis, String lockKey) {
		super(jedis, lockKey);
	}

	@Override
	public void lock() {
		while (true) {
			String result = jedis.set(lockKey, "value", NOT_EXIST);
			if (OK.equals(result)) {
				System.out.println(Thread.currentThread().getId() + "加锁成功!");
				break;
			}
		}
	}

	@Override
	public void unlock() {
		//解锁(释放锁)
		jedis.del(lockKey);
	}
}
